# @Author: 唐奇才
# @Time: 2021/5/5 15:30
# @File: mysqlUtils.py
# @Software: PyCharm

import pymysql

def updateData(data):
    try:
        conn = pymysql.connect(host="hadoop101", port=3306, user="root", password="1234", db="bilibili", )
        print("连接服务器成功!")
        # 2.获取游标对象
        cursor = conn.cursor()
        for d in data:

            # 3.执行sql语句
            sql = """
                update hotContent set hotLevel = '{}', playnum = '{}', relies = '{}' where oid = '{}'
            """.format(d[1], d[2], d[3],d[0])
            # 返回结果的数量
            count = cursor.execute(sql)
            print("结果数量: %s" % count)

            conn.commit()

    except Exception as e:
        print("连接服务器失败.....")
        print(type(e), e)
    finally:
        if cursor:
            cursor.close()
            print("关闭游标")
        if conn:
            conn.close()
            print("关闭链接")

# 1.建立连接
def getAllMysqlData():
    try:
        conn = pymysql.connect(host="hadoop101", port=3306, user="root", password="1234", db="bilibili", )
        print("连接服务器成功!")
        # 2.获取游标对象
        cursor = conn.cursor()
        # 3.执行sql语句
        sql = """
            select * from hotContent
        """
        # 返回结果的数量
        count = cursor.execute(sql)
        print("结果数量: %s" % count)
        # 获取所有内容
        myresult = cursor.fetchall()

    except Exception as e:
        print("连接服务器失败.....")
        print(type(e), e)
    finally:
        if cursor:
            cursor.close()
            print("关闭游标")
        if conn:
            conn.close()
            print("关闭链接")
        return myresult

def main():
    pass


if __name__ == '__main__':
    main()